package com.greenlife.controller.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;

import com.greenlife.commom.ResponseException;
import com.greenlife.common.enums.BaseResultCodeEnum;

/**
 *<p>team:yowosoft</p>
 * @author weipeng.xue
 *@date 2015年12月8日下午11:05:47
 * 
 */
public class Paging {
	
	/**
	 * @author weipeng.xue
	 * @date 2015年12月8日
	 * @param orderBy
	 * @return {@link Sort}
	 */
	public static Sort getPageSort(String orderBy){
		try{
			if(orderBy == null || orderBy.equals("")){
				return null;
			}
			String[] orderStrs = orderBy.split(";");
			List<Order> orderList = new ArrayList<Order>();
			for(String orderStr : orderStrs){
				String direction = orderStr.split(",")[1].toUpperCase();
				String item = orderStr.split(",")[0];
				Order order = new Order(Direction.valueOf(direction),item);
				orderList.add(order);
			}
			Sort sort = new Sort(orderList);
			return sort;
		}catch(Exception ex){
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_20111);
		}
	}
}
